﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
namespace huaguwang.hgmanage.Bogu
{
    public partial class BoGuGuPiao : System.Web.UI.Page
    {
        #region 固定方法
        D_T_StockCode _dtsc = new D_T_StockCode();
        D_T_StockCode_Exp _dtsc_exp = new D_T_StockCode_Exp();
        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 2:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                //case 4:
                //    but.Attributes.Add("onclick", "return GetAllCheckDefault();");
                //    break;
                default:
                    break;
            }
        }
        private huaguwang.WebControls.WebUserToolBar.SelectItem[] SetText()
        {
            huaguwang.WebControls.WebUserToolBar.SelectItem[] list = { 
                new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>TextBox2.Text.Exp_Trim(),
                    FuncSetText=V=>{TextBox2.Text=V;},
                    Name=TextBox2.ID
                },                                                                     
                   new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText=()=>TextBox1.Text.Exp_Trim(),
                    FuncSetText=V=>{TextBox1.Text=V;},
                    Name=TextBox1.ID
                },     
                                                                     
            };
            return list;
        }
        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {
                var txtGuName = this.TextBox1.Text.Exp_Trim();
                var txtGuCode = this.TextBox2.Text.Exp_Trim();
                Pager.RecordCount = _dtsc_exp.GetOneTablePageCount(txtGuName, txtGuCode);




                this.Repeater1.DataSource = _dtsc_exp.GetOneTablePage(begin, Pager.PageSize, txtGuName, txtGuCode);
                this.Repeater1.DataBind();
            });
        }

        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                Pager.PageSize = 10;
                //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
                this.WebUserToolBar1.MainInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr,
                    SetText()
                );


                if (!IsPostBack)
                {
                    //分页查询
                    SetData(0);
                }
            });

        }
        #region 基本的带有查询的事件组合

        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AdminPublic.Try(() =>
            {
                SetData((e.NewPageIndex - 1) * Pager.PageSize);
            });
        }

        /// <summary>
        /// 股票同步信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
          {
              var error = string.Empty;
              //获取股票同步信息
              if (_dtsc_exp.ExcuteGetGuInfo(out error))
              {
                  this.Exp_PageAlter("股票同步信息成功!");
              }
              else
              {
                  this.Exp_PageAlter(error);
              }
          });
        }
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                Response.Redirect("BoGuGuPiao_E.aspx?" + sKey, false);
            });
        }

        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var sKey = WebUserToolBar1.MainSetSKey();
                Response.Redirect("BoGuGuPiao_E.aspx?ID=" + strID + "&" + sKey, false);
            });
        }
        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
                if (intA == null || intA.Count <= 0)
                {
                    this.Exp_PageAlter("没有找到可以删除的信息");
                    return;
                }

                int i = 0;
                var refInt = _dtsc.UpdateFlag(intA);
                if (refInt > 0)
                {
                    this.Exp_PageWriteLog(Log_Type.delete, "股票资料维护", intA);
                    SetData(0);
                    this.Exp_PageAlter("删除操作成功");
                    return;
                }
                else
                {
                    this.Exp_PageAlter("删除操作失败");
                    return;
                }
            });
        }
        #endregion
        /// <summary>
        /// 搏股计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button4(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
              {
                  DataTable _dt = new DataTable();
                  _dtsc_exp.ExcuteAccountBoGuInfo(UserConst.Exp_GetUser(this).id, out _dt);
                  StringBuilder _sb = new StringBuilder();
                  if (_dt != null && _dt.Rows.Count > 0)
                  {
                      foreach (DataRow _dr in _dt.Rows)
                      {
                          var alertstring = string.Empty;
                          if (_dr["state"].ToString() == "1")
                          {
                              alertstring = "成功";
                          }
                          else
                          {
                              alertstring = "失败";
                          }
                          _sb.Append("搏股ID：" + _dr["id"].ToString() + " 主题：" + _dr["title"].ToString() + " 状态：" + alertstring);
                          _sb.Append("\\n");
                      }
                      _sb.Append("计算完毕，谢谢！");
                      this.Exp_PageAlter(_sb.ToString());
                  }
                  else
                  {
                      this.Exp_PageAlter("无数据需要计算，谢谢！");
                  }
              });
        }

        /// <summary>
        /// 一键同步计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button5(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
          {
              var error = string.Empty;
              //获取股票同步信息
              if (_dtsc_exp.ExcuteGetGuInfo(out error))
              {
                  this.Exp_PageAlter("股票同步信息成功，谢谢！");
                  DataTable _dt = new DataTable();
                  _dtsc_exp.ExcuteAccountBoGuInfo(UserConst.Exp_GetUser(this).id, out _dt);
                  StringBuilder _sb = new StringBuilder();
                  if (_dt != null && _dt.Rows.Count > 0)
                  {
                      foreach (DataRow _dr in _dt.Rows)
                      {
                          var alertstring = string.Empty;
                          if (_dr["state"].ToString() == "1")
                          {
                              alertstring = "成功";
                          }
                          else
                          {
                              alertstring = "失败";
                          }
                          _sb.Append("搏股ID：" + _dr["id"].ToString() + " 主题：" + _dr["title"].ToString() + " 状态：" + alertstring);
                          _sb.Append("\\n");
                      }
                      this.Exp_PageAlter(_sb.ToString());
                  }
              }
              else
              {
                  this.Exp_PageAlter(error);
              }
          });
        }

        protected void WebUserToolBar1_Button6(object sender, EventArgs e)
        {
            SetData(0);
        }
    }
}
